
#include "includes.h"
static  OS_STK      AppTaskStartStk[APP_TASK_START_STK_SIZE];
static  OS_STK      AppTaskLEDStk[APP_TASK_LED_STK_SIZE];

#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
int a;
a=0;
a=a+1;
}
#endif
unsigned long _ulRuntimeMs=0;
const int _ciSystickMs=500;

/*
*********************************************************************************************************
*                                            FUNCTION PROTOTYPES
*********************************************************************************************************
*/

static  void   AppTaskCreate                (void);
static  void   AppTaskStart                 (void *p_arg);
static  void   AppTaskUserIF                (void);
static  void   AppTaskLED                   (void *p_arg);
static  void   AppDispScr_VersionTickRate   (void);

static  void   AppFormatDec                 (CPU_INT08U *s, CPU_INT32U value, CPU_INT08U digits);

void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
    //
    // Loop while there are more characters to send.
    //
    while(ulCount--)
    {
        //
        // Write the next character to the UART.
        //
        UARTCharPutNonBlocking(UART1_BASE, *pucBuffer++);
    }
}
measureADC (){
    UARTCharPutNonBlocking(UART1_BASE, 'v');
  unsigned long ulValue;
  //
  // Trigger the sample sequence.
  //
  ADCProcessorTrigger(ADC_BASE, 0);
  //
  // Wait until the sample sequence has completed.
  //
  while(!ADCIntStatus(ADC_BASE, 0, false)){}
  // Read the value from the ADC.
  //
  ADCSequenceDataGet(ADC_BASE, 0, &ulValue);
}
  
// The UART interrupt handler.
void UART1IntHandler(void)
{
    unsigned long ulStatus;

    //
    // Get the interrrupt status.
    //
    ulStatus = UARTIntStatus(UART1_BASE, true);

    //
    // Clear the asserted interrupts.
    //
    UARTIntClear(UART1_BASE, ulStatus);

    //
    // Loop while there are characters in the receive FIFO.
    //
    while(UARTCharsAvail(UART1_BASE))
    {
        //
        // Read the next character from the UART and write it back to the UART.
        //
        UARTCharPutNonBlocking(UART1_BASE, UARTCharGetNonBlocking(UART1_BASE));
        UARTCharPutNonBlocking(UART1_BASE, 'a');
    }
}
    
static void AppTaskStart (void *p_arg)
{
    CPU_INT08U  i;
    (void)p_arg;

    BSP_Init();                                                 /* Initialize BSP functions                                 */

#if OS_TASK_STAT_EN > 0
    OSStatInit();                                               /* Determine CPU capacity                                   */
#endif
    AppTaskCreate();
    unsigned long ulRow, ulCol;
    volatile int iDelay;
    static unsigned char pucBuf[64];
    static char pucHello[] =
    {
        "                      "
        "See How Far Your Dollar Can Go!"
        "                      "
    };
/*
    //
    // Set GPIO A0 and A1 as UART pins.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    GPIOPinTypeUART(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3);

    //
    // Configure the UART for 2400, 8-N-1 operation.
    //
    UARTConfigSetExpClk(UART1_BASE, SysCtlClockGet(), 2400,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_TWO |
                         UART_CONFIG_PAR_EVEN));

    //
    // Enable the UART interrupt.
    //
    IntEnable(INT_UART1);  
    //UARTIntEnable(UART1_BASE, UART_INT_RX | UART_INT_RT);
      
    //
    // Enable the first sample sequence to capture the value of channel 0 when
    // the processor trigger occurs.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC);
    ADCSequenceConfigure(ADC_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceStepConfigure(ADC_BASE, 0, 0, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH0);
    ADCSequenceEnable(ADC_BASE, 0);
*/
    //
    // Simple scrolling text display
    //
    ulCol = 0;
    while (DEF_TRUE)                                          /* Task body, always written as an infinite loop.           */
    {
        //
        // Display the text.
        //
        RIT128x96x4StringDraw(&pucHello[ulCol++], 0, 0, 11);
        AppDispScr_VersionTickRate();
        OSTimeDly(OS_TICKS_PER_SEC / 10);

        //
        // Wrap the index back to the beginning of the string.
        //
        if(ulCol > 53)
        {
            ulCol = 0;
        }
    }
}

static void AppTaskLED (void *p_arg){
    CPU_INT08U  i;
    (void)p_arg;

    //
    // Enable the GPIO port that is used for the on-board LED.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    //
    // Enable the GPIO pin for the LED (PF0).  Set the direction as output, and
    // enable the GPIO pin for digital function.
    //
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0);
    double d=0;
    double r=0;
    double t=0;
    char    s[50];
    //
    // Loop forever.
    //
    while(1)
    {
        //
        // Turn on the LED.
        //
        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 1);
        for (int i=1;i<2000;i++){d+=1.0/i;}
        //
        // Delay for a bit.
        //
        OSTimeDly(OS_TICKS_PER_SEC / 10);
        for (int i=1;i<1000;i++){r+=1.0/i;}
        //
        // Turn off the LED.
        //
        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0);

        //
        // Delay for a bit.
        //
        OSTimeDly(OS_TICKS_PER_SEC / 10);
        t=r+d;
        s[0] = ((int)t %  10) + '0';
        s[1]=0;
        //sprintf(s,"d:%2.2f r:%2.2f .",d,r);
        RIT128x96x4StringDraw((char const *)s, 0, 70, 11);
    }
}

static  void  AppTaskCreate (void)
{
#if OS_TASK_NAME_SIZE > 1
    CPU_INT08U  err;
#endif


    OSTaskCreateExt(AppTaskLED,
                    (void *)0,
                    (OS_STK *)&AppTaskLEDStk[APP_TASK_LED_STK_SIZE - 1],
                    APP_TASK_LED_PRIO,
                    APP_TASK_LED_PRIO,
                    (OS_STK *)&AppTaskLEDStk[0],
                    APP_TASK_LED_STK_SIZE,
                    (void *)0,
                    OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);

#if OS_TASK_NAME_SIZE > 1
    OSTaskNameSet(APP_TASK_LED_PRIO, "LED Task", &err);
#endif
}

int main(void)
{
    #if OS_TASK_NAME_SIZE > 1
        CPU_INT08U  err;
    #endif
    
        BSP_IntDisAll();                                            /* Disable all interrupts until we are ready to accept them */
        OSInit();                                                   /* Initialize "uC/OS-II, The Real-Time Kernel"              */
        OSTaskCreateExt(AppTaskStart,                               /* Create the start task                                    */
                        (void *)0,
                        (OS_STK *)&AppTaskStartStk[APP_TASK_START_STK_SIZE - 1],
                        APP_TASK_START_PRIO,
                        APP_TASK_START_PRIO,
                        (OS_STK *)&AppTaskStartStk[0],
                        APP_TASK_START_STK_SIZE,
                        (void *)0,
                        OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
    
    #if OS_TASK_NAME_SIZE > 1
        OSTaskNameSet(APP_TASK_START_PRIO, "Start Task", &err);
    #endif
    
    OSStart();                                                  /* Start multitasking (i.e. give control to uC/OS-II)  */
}



/*
*********************************************************************************************************
*                                            DISPLAY SCREENS
*********************************************************************************************************
*/

static  void  AppDispScr_VersionTickRate (void)
{
    CPU_INT08U    s[20];
    CPU_INT32U    value;
    
    OS_StrCopy(s, "uC/OS-II:  Vx.yy");
    value = (CPU_INT32U)OSVersion();
    s[12] = value / 100 + '0';
    s[14] = (value % 100) / 10 + '0';
    s[15] = (value %  10) + '0';
    
    RIT128x96x4StringDraw((char const *)s, 0, 10, 11);

    OS_StrCopy(s, "TickRate:   xxxx");
    value = (CPU_INT32U)OS_TICKS_PER_SEC;
    AppFormatDec(&s[12], value, 4);
    s[16] = 0;
    RIT128x96x4StringDraw((char const *)s, 0, 20, 11);

#if OS_TASK_STAT_EN > 0
    OS_StrCopy(s, "CPU Usage:xx %  ");
    value = (CPU_INT32U)OSCPUUsage;
    s[10] = (value / 10) + '0';
    s[11] = (value % 10) + '0';
    RIT128x96x4StringDraw((char const *)s, 0, 30, 11);
#endif

    OS_StrCopy(s, "CPU Speed:xx MHz");
    value = (CPU_INT32U)SysCtlClockGet() / 1000000L;
    s[10] = (value / 10) + '0';
    s[11] = (value % 10) + '0';
    RIT128x96x4StringDraw((char const *)s, 0, 40, 11);

#if OS_TIME_GET_SET_EN > 0
    OS_StrCopy(s, "#Ticks: xxxxxxxx");
    value = (CPU_INT32U)OSTime;
    AppFormatDec(&s[8], value, 8);
    RIT128x96x4StringDraw((char const *)s, 0, 50, 11);
#endif

    OS_StrCopy(s, "#CtxSw: xxxxxxxx");
    value = (CPU_INT32U)OSCtxSwCtr;
    AppFormatDec(&s[8], value, 8);
    RIT128x96x4StringDraw((char const *)s, 0, 60, 11);
}

/*
*********************************************************************************************************
*                                      FORMAT A DECIMAL VALUE
*
* Description: This function converts a decimal value to ASCII (with leading zeros)
* Arguments  : s       is a pointer to the destination ASCII string
*              value   is the value to convert (assumes an unsigned value)
*              digits  is the desired number of digits
*********************************************************************************************************
*/

static  void  AppFormatDec (CPU_INT08U *s, CPU_INT32U value, CPU_INT08U digits)
{
    CPU_INT08U   i;
    CPU_INT32U   mult;


    mult = 1;
    for (i = 0; i < (digits - 1); i++) {
        mult *= 10;
    }
    while (mult > 0) {
        *s++   = value / mult + '0';
        value %= mult;
        mult  /= 10;
    }
    *s = 0;
}